home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9110.ZIP / STRING.ZIP / STRIINS1.CPP < prev    next >
C/C++ Source or Header  |  1991-07-30  |  705b  |  31 lines

  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <string.hpp>
  5.  
  6. String &String::insert(int pos, const String &a)
  7. {
  8.     if (!a.length())
  9.         return *this;
  10.     if (pos > length()) { // pos == length() is concatenate
  11.         errno = ERANGE;
  12.         return *this;
  13.     }
  14.     int n = length()+a.length();
  15.     srep *p = new(n) srep(n);
  16.     if (!p) {
  17.         errno = ENOMEM;
  18.         return *this;
  19.     }
  20.     memmove(p->body,body(),pos);
  21.     memmove(p->body+pos, a.body(), a.length());
  22.     memmove(p->body+pos+a.length(), body()+pos, length()-pos);
  23.     if (rp) {
  24.         if (--rp->refs < 1)
  25.         delete rp;
  26.     }
  27.     rp = p;
  28.     rp->refs++;
  29.     return *this;
  30. }
  31.